package com.kingyun.gpsinspection.purificationservice.services.som.innerservice.impl;

import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.AdmittanceMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.SomFileMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.StationInMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.StationOutMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.File;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Employee;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.Admittance;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.StationIn;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.StationOut;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.OutEmployeeService;
import com.kingyun.gpsinspection.purificationservice.facade.service.som.InOutService;
import com.kingyun.gpsinspection.purificationservice.facade.service.som.InfoflowService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 进出站接口实现类
 * Created by yangcs on 2017/3/27.
 */
public class InOutServiceImpl implements InOutService {

    private static Logger logger = LoggerFactory.getLogger(InOutServiceImpl.class);

    @Autowired
    private StationInMapper stationInMapper;
    @Autowired
    private AdmittanceMapper admittanceMapper;
    @Autowired
    private SomFileMapper somFileMapper;
    @Autowired
    private StationOutMapper stationOutMapper;
    @Autowired
    private OutEmployeeService outEmployeeService;
    @Autowired
    private InfoflowService infoflowService;

    /**
     * 根据组织id查询进出站信息
     * @param orgId
     * @return
     */
    @Override
    public List<StationIn> queryByOrgId(String orgId) {
        return stationInMapper.queryByOrgId(orgId);
    }

    /**
     * 验证进站
     * @param stationIn
     * @return
     */
    @Override
    public Admittance authCode(StationIn stationIn) {
        return admittanceMapper.queryAdmittance(stationIn);
    }

    /**
     * 查询单个进出站信息
     * @param stationIn
     * @return
     */
    @Override
    public StationIn queryStationIn(StationIn stationIn) {
        return stationInMapper.queryStationIn(stationIn);
    }

    /**
     * 新增进出站信息
     * @param admittance
     * @param stationIn
     * @return
     */
    @Override
    public StationIn insert(Admittance admittance,StationIn stationIn) {
        stationIn.setStationInId(UniqueUtil.uuid());
        int result = stationInMapper.insert(stationIn);
        if( result < 1 ){
            return null;
        }else{
            stationIn.setStartTime(admittance.getStartTime());
            stationIn.setFinishTime(admittance.getFinishTime());
            stationIn.setApplyCompany(admittance.getApplyCompany());
            stationIn.setApplyMan(admittance.getApplyMan());
            stationIn.setApplyManPhone(admittance.getApplyManPhone());
            stationIn.setStationInReason(admittance.getStationInReason());
            return stationIn;
        }
    }

    /**
     * 新增进出站信息
     * @param stationIn
     * @return
     */
    @Override
    public StationIn insert(StationIn stationIn) {
        stationIn.setStationInId(UniqueUtil.uuid());
        stationIn.setAdmittanceId(UniqueUtil.uuid());
        Employee employee = outEmployeeService.findById(stationIn.getCreateUserId());
        stationIn.setApplyManPhone(StringUtils.isNotBlank(employee.getMobilePhone())?employee.getMobilePhone():"-");
        admittanceMapper.insertByStation(stationIn);
        int result = stationInMapper.insert(stationIn);
        if( result < 1 ){
            return null;
        }else{
            return stationIn;
        }
    }

    /**
     * 进站确认
     * @param stationIn
     * @return
     */
    @Override
    public StationIn sure(StationIn stationIn) {
        List<File> list = stationIn.getFiles();
        File fileInfo = new File();
        File file;
        for( int i=0;i< list.size();i++){
            file = list.get(i);
            file.setBusinessId(stationIn.getStationInId());
            file.setFileId(UniqueUtil.uuid());
            file.setFileType(File.IMG_TYPE);
            if( i == 0 ){
                fileInfo = file;
            }
            somFileMapper.insert(file);
        }

        try {
            StationIn params = new StationIn();
            params.setStationInId(stationIn.getStationInId());
            infoflowService.addInfoflowByInOut( queryStationIn(params),fileInfo );
        } catch (Exception e) {
            logger.error("进站确认时添加动态失败"+e.getMessage());
            e.printStackTrace();
        }

        // 更新进出站信息状态
        if(  stationIn.getManNumb() > 0){
            stationIn.setRestNumb(stationIn.getManNumb());
        }
        stationInMapper.updateStatus(stationIn);
        return stationInMapper.queryStationIn(stationIn);
    }

    /**
     * 出站
     * @param stationIn
     * @param leaveNumb
     * @param userId
     * @return
     */
    @Override
    public StationIn out(StationIn stationIn, int leaveNumb,String userId) {
        // 出站人数和剩余人数相同
        if( stationIn.getRestNumb() == leaveNumb ){
            stationIn.setStatus(2);
        }else{
            stationIn.setStatus(1);
        }
        stationIn.setRestNumb(stationIn.getRestNumb()-leaveNumb);
        stationIn.setModifyUserId(userId);
        // 更新进出站信息状态
        int result = stationInMapper.updateStatus(stationIn);
        if( result > 0 ){
            // 添加出站日志
            StationOut stationOut = new StationOut();
            stationOut.setStationOutId(UniqueUtil.uuid());
            stationOut.setStationInId(stationIn.getStationInId());
            stationOut.setCreateUserId(userId);
            stationOut.setOutNumber(leaveNumb);
            result = stationOutMapper.insert(stationOut);
        }
        if( result > 0 ){
            // 查询该次准入信息的所有进出站信息是否都已经完成
            result = admittanceMapper.queryRestStatin(stationIn.getStationInId());
            // 全部完成 则修改
            if( result == 0 ){
                stationIn.setStatus(5);
                admittanceMapper.updateStatus(stationIn);
            }
        }
        return stationIn;
    }
}
